package com.yjps.house.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.yjps.common.message.Message;
import com.yjps.common.util.TimeUtil;
import com.yjps.house.model.House;
import com.yjps.house.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/house")
public class HouseController {

    @Autowired
    HouseService houseService;

    /**
     * 更新房源信息
     * @param json "house": { "id":"","buildingId": "", "title": "", "description": "", "imageUrl": "", "area": "", "price": "", "sellType": "", "detail": "" }
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestParam(value = "json") String json){
        House house = JSON.parseObject(json, House.class);
        boolean flag=houseService.updatehouse(house);
        Message message;
        if (flag){
            message=new Message(flag,"更新操作成功");
        }else{
            message=new Message(flag,"存在四种情况无法修改");
        }
        return message.toJson();
    }


    /**
     * 门店经理审核房源
     * @param json{"houseId": "","handleResult": ""}
     * @return
     */
    @RequestMapping(value = "/verify",method = RequestMethod.POST)
    @ResponseBody
    public String verify(@RequestParam(value = "json") String json){
        //解析json
        JSONObject object= JSON.parseObject(json);
        int id=object.getInteger("houseId");
        int handleResult=Integer.parseInt(object.getString("handleResult"));
        String handleTime= TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);

        //调用业务
        boolean flag=houseService.verfiyHouse(id, handleTime, handleResult);

        //封装返回
        Message message;
        if(flag){
            message=new Message(true,"审核操作成功");
        }else{
            message=new Message(false,"审核操作失败");
        }
        return message.toJson();
    }


    /**
     * 门店经理确定是否为精品房源
     * @param json{"houseId": "","handleResult": ""}
     * @return
     */
    @RequestMapping(value = "/recommend",method = RequestMethod.POST)
    @ResponseBody
    public String recommend(@RequestParam(value = "json") String json){
        //解析json
        JSONObject object= JSON.parseObject(json);
        int id=object.getInteger("houseId");
        int handleResult=Integer.parseInt(object.getString("handleResult"));
        String handleTime= TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);

        //调用业务
        boolean flag=houseService.recommendHouse(id, handleTime, handleResult);

        //封装返回
        Message message;
        if(flag){
            message=new Message(true,"精品确认操作成功");
        }else{
            message=new Message(false,"精品确认操作失败");
        }
        return message.toJson();
    }

    /**
     * 条件查询房源
     * @param json {"address":{city:"",county:"",town:""},"house":{house_area:"",house_sell_type:"",house_is_boutique:"",house_authentication:""},page:"",size:""}
     * @return
     */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public String queryHouseList(@RequestParam(value = "json") String json){
        //解析json
        JSONObject object= JSON.parseObject(json);
        int size=Integer.parseInt(object.getString("size"));
        int startRow=(Integer.parseInt(object.getString("page"))-1)*size;
        Map<String,Object> addressMap=(Map<String,Object>)JSON.parse(object.getString("address"));
        Map<String,Object> houseMap=(Map<String,Object>)JSON.parse(object.getString("house"));

        //调用业务
        List<House> houseList=houseService.queryHouse(houseMap, addressMap, startRow, size);

        //封装
        Message message;
        if (!houseList.isEmpty()){
            message=new Message(true,"查询成功",houseList);
        }else{
            message=new Message(false,"查询失败");
        }
        return message.toJson();
    }
}
